/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-07-15
 * 15:37
 */
public class MyLinkedList implements Ilist {

    public class ListNode {
        int val;
        ListNode prev;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

        ListNode head;
        ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode nnode=new ListNode(data);
        if(head==null){
            head=last=nnode;
        }else {
            nnode.next=head;
            head.prev=nnode;
            head=nnode;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode nnode=new ListNode(data);
        if(head==null){
            head=last=nnode;
        }else{
            nnode.prev=last;
            last.next=nnode;
            last=nnode;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        int len=size();
        if(index<0||index>len){
            System.out.println("位置不合法");
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }if(index==len){
            addLast(data);
            return;
        }
        ListNode cur=head;
        ListNode indexdata=new ListNode(data);
        while (index-1!=0){
            cur=cur.next;
            index--;
        }
        indexdata.next=cur.next;
        indexdata.prev=cur;
        cur.next.prev=indexdata;
        cur.next=indexdata;
    }

    @Override
    public boolean contains(int key) {
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    head.prev=null;
                    return;
                }
                if(cur==last){
                    cur.prev.next=cur.next;
                    last=last.prev;
                    return;
                }else {
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                    return;
                }
            }else {
                cur=cur.next;
            }
        }

    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
                if(head==cur){
                    head=head.next;
                   if(head!=null){
                       head.prev=null;
                   }
                    cur=head;
                }else{
                    if(cur==last){
                        last=last.prev;
                        last.next=null;
                        cur=last;
                    }else {
                        cur.prev.next=cur.next;
                        cur.next.prev=cur.prev;
                        cur=cur.next;
                    }

                }
            }else{
                cur=cur.next;
            }
        }

    }

    @Override
    public int size() {
        int len=0;
        ListNode cur=head;
        while (cur!=null){
            cur=cur.next;
            len++;
        }
        return len;
    }

    @Override
    public void display() {
        ListNode cur=head;
        if(cur==null){
            System.out.println("链表为空");
            return;
        }
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        head=null  ;

    }
}
